package com.example.interview.ch03;

/**
 * synchronized(任意自定义对象)与synchronized同步方法共用
 *
 * @author anin
 * @date 2022/4/27 10:18 AM
 */
public class Test2 {

    /*
     * 锁不是同一个，不会顺序执行
     */

    public static void main(String[] args) {
        Test2ObjectService objectService = new Test2ObjectService();
        new Thread(()->{
            objectService.methodA();
        }).start();
        new Thread(()->{
            objectService.methodB();
        }).start();
    }
}


class Test2ObjectService {
    private String lock=new String();
    public void methodA(){
        try {
            synchronized (lock) {
                System.out.println("a begin");
                Thread.sleep(3000);
                System.out.println("a   end");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public synchronized void methodB(){
        System.out.println("b begin");
        System.out.println("b   end");
    }
}
